package com.witframework.web.tag.timer;


import java.util.Hashtable;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

public class TimerTag extends BodyTagSupport
{
  PageContext pageContext;
  private static final String DEFAULT_ID = "dflttmrdcj2008";
  private static final String TIMERTAG = "trmtgcj2008";
  private String id = null;
  private String scope = "application";
  private int interval = 0;
  private String sBody = null;

  public void setId(String paramString){
    this.id = paramString;
  }

  public String getId(){
    return this.id;
  }

  public void setScope(String paramString){
    this.scope = paramString;
  }

  public String getScope(){
    return this.scope;
  }

  public void setInterval(int paramInt){
    this.interval = paramInt;
  }
  public int getInterval(){
    return this.interval;
  }

  public void setPageContext(PageContext paramPageContext){
    this.pageContext = paramPageContext;
  }

  public PageContext getPageContext(){
    return this.pageContext;
  }

  public int doStartTag() throws JspException{
    if (this.id == null)
      this.id = "dflttmrdcj2008";
    if (this.interval <= 0)
      return 2;
    if (!(checkCode(this.id, this.scope, this.interval)))
      return 0;
    return 2;
  }

  public int doAfterBody() throws JspException{
    BodyContent localBodyContent = getBodyContent();
    if (localBodyContent != null){
      this.sBody = localBodyContent.getString();
      localBodyContent.clearBody();
      JspWriter localJspWriter = localBodyContent.getEnclosingWriter();
      try
      {
        localJspWriter.print(this.sBody);
      }
      catch (Exception localException)
      {
        throw new JspTagException("timer tag: Could not write body");
      }
    }
    if (this.sBody == null)
      this.sBody = "";
    return 0;
  }

  public int doEndTag() throws JspException{
    Hashtable localHashtable = getCodeHash();
    if (this.interval > 0)
      synchronized (localHashtable)
      {
        CodeBean localCodeBean = (CodeBean)localHashtable.get(this.id);
        if (localCodeBean != null)
        {
          if (this.sBody != null)
            localCodeBean.setContent(this.sBody);
          else
            this.sBody = localCodeBean.getContent();
          localCodeBean.setMark(false);
        }
      }
    if (this.sBody == null)
      this.sBody = "";
    if (this.id != null)
      this.pageContext.setAttribute(this.id, this.sBody, 1);
    dropData();
    return 6;
  }

  public void release()
  {
    dropData();
  }

  private void dropData()
  {
    this.id = null;
    this.scope = "application";
    this.interval = 0;
    this.sBody = null;
  }

  private boolean checkCode(String paramString1, String paramString2, int paramInt){
    Hashtable localHashtable = getCodeHash();
    synchronized (localHashtable)
    {
      long l = System.currentTimeMillis();
      CodeBean localCodeBean = (CodeBean)localHashtable.get(paramString1);
      if (localCodeBean == null)
      {
        localCodeBean = new CodeBean();
        localCodeBean.setMark(true);
        localCodeBean.setTime(l);
        localHashtable.put(paramString1, localCodeBean);
        return true;
      }
      if (localCodeBean.getMark())
        return false;
      if (l - localCodeBean.getTime() < 1000 * paramInt)
        return false;
      localCodeBean.setMark(true);
      localCodeBean.setTime(l);
      return true;
    }
  }

  private void markCode(String paramString1, String paramString2){
    Hashtable localHashtable = getCodeHash();
    synchronized (localHashtable)
    {
      CodeBean localCodeBean = (CodeBean)localHashtable.get(paramString1);
      if (localCodeBean == null)
      {
        localCodeBean = new CodeBean();
        localHashtable.put(paramString1, localCodeBean);
      }
      localCodeBean.setMark(false);
    }
  }

  private Hashtable getCodeHash(){
    Hashtable localHashtable = null;
    if ("application".equalsIgnoreCase(this.scope))
    {
      localHashtable = (Hashtable)this.pageContext.getAttribute("trmtgcj2008", 4);
      if (localHashtable == null)
      {
        localHashtable = new Hashtable();
        this.pageContext.setAttribute("trmtgcj2008", localHashtable, 4);
      }
    }
    else
    {
      localHashtable = (Hashtable)this.pageContext.getAttribute("trmtgcj2008", 3);
      if (localHashtable == null)
      {
        localHashtable = new Hashtable();
        this.pageContext.setAttribute("trmtgcj2008", localHashtable, 3);
      }
    }
    return localHashtable;
  }
}
